package com.action.app_page;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;

import com.enetiy.Urls;
import com.enetiy.Urlsver;
import com.enetiy.Version;
import com.service.UrlService;
import com.service.VersionService;
import com.util.DataUtil;
import com.util.IPUtil;

public class UrlAction {
	private int areaid;
	private UrlService urlService;
	private VersionService versionService;
	private int appType;
	private String params;
	
	private String ver;
	private int vertype;
	
	//第一个请求（框架底部和顶部包括下拉框图标和功能）
	public void getlinks() {
		//json解析对象
		if(params!=null&&!params.equals("")){
			org.json.JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("areaid"))
				areaid = datajson.getInt("areaid");
			if(datajson.has("appType"))
				appType = datajson.getInt("appType");
			if(datajson.has("ver"))
				ver = datajson.getString("ver");
			if(datajson.has("vertype"))
				vertype = datajson.getInt("vertype");
			if(areaid==0){
				areaid=90;
			}
		}
		if(appType==0){
			appType=1;
		}
		if(ver==null||ver.equals("")||vertype==0){
			ver="1.6";
			vertype=1;
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		JSONObject sendjson = DataUtil.sendJson();
		
		JSONArray bottomList = new JSONArray();
		JSONArray topList = new JSONArray();
		JSONArray comboBoxList = new JSONArray();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			Version version = versionService.selectVersion(ver, vertype,appType);
			if(version!=null&&version.getStatus()!=0){
					//第一个请求内容
					List<Urlsver> list = urlService.sleUrlsList(1,areaid,1,version.getId());
					if(!list.isEmpty()){
						for (int i = 0; i <list.size(); i++){
							 Urls urls = list.get(i).getUrls();
							
							JSONObject listdata = new JSONObject();
							listdata.put("type", urls.getUrlstype().getDiction());
							listdata.put("title", urls.getUrlText());
							listdata.put("icon", this.icompath(urls.getPic(),"frameIcon"));
							listdata.put("selectedIcon", this.icompath(urls.getClickpic(),"frameIcon"));
							listdata.put("order", list.get(i).getUrlOrder());
							listdata.put("url", urls.getUrl());
							//框架底部
							if(urls.getPorstion()==1){
								bottomList.put(listdata);
							}
							//框架顶部
							if(urls.getPorstion()==2){
								topList.put(listdata);
							}
							//下拉框
							if(urls.getPorstion()==3){
								comboBoxList.put(listdata);
							}
								if(urls.getPorstion()==9){
							sendjson.put("appname", urls.getUrlText());
						}
					}
						sendjson.put("code", 1);
						sendjson.put("bottomTabBar", bottomList.toString());
						sendjson.put("topTapBar", topList.toString());
						sendjson.put("comboBoxTapBar",comboBoxList.toString());
						
					}else{
						sendjson.put("code", 0);
						sendjson.put("bottomTabBar", bottomList);
						sendjson.put("topTapBar", topList);
						sendjson.put("comboBoxTapBar",comboBoxList );
						sendjson.put("msg","没有数据" );
					}
					//返回
				}else{
					sendjson.put("code",0);
					sendjson.put("bottomTabBar", bottomList);
					sendjson.put("topTapBar", topList);
					sendjson.put("comboBoxTapBar",comboBoxList );
					sendjson.put("msg","版本号不存在" );
				}
			
			
				List<Integer> noticeList = new ArrayList<Integer>();
				noticeList.add(10000);
				JSONArray noticeArea = new JSONArray(noticeList);
				
				sendjson.put("noticeArea", noticeArea.toString());
				sendjson.put("noticeId", "10003");
				sendjson.put("noticeUrl", "http://119.29.54.250:8090/NewFiPortal/moportal/notice.do");
			
			} catch (Exception e) {
				sendjson.put("code", -1);
				sendjson.put("bottomTabBar", bottomList);
				sendjson.put("topTapBar", topList);
				sendjson.put("comboBoxTapBar",comboBoxList );
				e.printStackTrace();
			}
		out.print(sendjson);
	}

	private String icompath(String path,String type){
		String p="";
		
		if(path!=null&&!path.equals("")){
			if(type.equals("frameIcon"))
				p=IPUtil.getIp()+"/icon/appimg/frameIcon/"+path;
			if(type.equals("homepageIcon"))
				p=IPUtil.getIp()+"/icon/appimg/homepageIcon/"+path;
		}
		return p;
	}
	/**
	 * app数据第二个请求  >Nf016
	 * 
	 */
	public void pageHome(){
		if(params!=null&&!params.equals("")){
			System.out.println(params);
			org.json.JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("areaid"))
				areaid = datajson.getInt("areaid");
			if(datajson.has("appType"))
				appType = datajson.getInt("appType");
			if(datajson.has("ver"))
				ver = datajson.getString("ver");
			if(datajson.has("vertype"))
				vertype = datajson.getInt("vertype");
			
			if(areaid==0){
				areaid=90;
			}
		}
		if(appType==0){
			appType=1;
		}
		if(ver==null||ver.equals("")||vertype==0){
			ver="1.6";
			vertype=1;
		}
		
		
		JSONObject sendjson = DataUtil.sendJson();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		JSONObject topPagehome =new JSONObject();
		
		JSONArray topTextList = new JSONArray();
		JSONArray topPictureList = new JSONArray();
		JSONArray centreList = new JSONArray();
		JSONArray bottomIconList = new JSONArray();
		JSONArray bottomPictureList = new JSONArray();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			//第二个请求内容
			Version version = versionService.selectVersion(ver, vertype,appType);
			if(version!=null&&version.getStatus()!=0){
				List<Urlsver> list = urlService.sleUrlsList(2,areaid,1,version.getId());
				if(!list.isEmpty()){
					for (int i = 0; i <list.size(); i++){
						 Urls urls = list.get(i).getUrls();
							JSONObject listdata = new JSONObject();
							listdata.put("type", urls.getUrlstype().getDiction());
							listdata.put("text", urls.getUrlText());
							listdata.put("pic", this.icompath(urls.getPic(),"homepageIcon"));
							listdata.put("order", list.get(i).getUrlOrder());
							listdata.put("url", urls.getUrl());
						if(urls.getPorstion()==4){
							topTextList.put(listdata);
						}
						if(urls.getPorstion()==5){
							topPictureList.put(listdata);
						}
							
						if(urls.getPorstion()==6){
							centreList.put(listdata);
						}
							
						if(urls.getPorstion()==7){
							bottomIconList.put(listdata);
						}
						if(urls.getPorstion()==8){
							bottomPictureList.put(listdata);
						}
					}
					
					topPagehome.put("textModels", topTextList.toString());
					topPagehome.put("picModels", topPictureList.toString());
					
					sendjson.put("code", 1);
					sendjson.put("bannerModels", topPagehome);
					sendjson.put("featureModels", centreList.toString());
					sendjson.put("hotSiteModels",bottomIconList.toString());
					sendjson.put("adModels", bottomPictureList.toString());
				}else{
					sendjson.put("code", 0);
					sendjson.put("bannerModels", topPagehome);
					sendjson.put("featureModels", centreList.toString());
					sendjson.put("hotSiteModels",bottomIconList.toString());
					sendjson.put("adModels", bottomPictureList.toString());
					sendjson.put("msg","没有数据" );
				}
			}else{
				sendjson.put("code", 0);
				sendjson.put("bannerModels", topPagehome);
				sendjson.put("featureModels", centreList.toString());
				sendjson.put("hotSiteModels",bottomIconList.toString());
				sendjson.put("adModels", bottomPictureList.toString());
				sendjson.put("msg","版本号不存在" );
			}
		} catch (Exception e) {
			sendjson.put("code", -1);
			sendjson.put("bannerModels", topPagehome);
			sendjson.put("featureModels", centreList.toString());
			sendjson.put("hotSiteModels",bottomIconList.toString());
			sendjson.put("adModels", bottomPictureList.toString());
			e.printStackTrace();
		}
		out.print(sendjson);
		System.out.println(sendjson.toString());
	}
	
	
	/**
	 * 
	 * 排序
	 *//*
	private static List mySort(List<Urls> list) { // 交换排序->冒泡排序
		Urls temp = null;
		boolean exchange = false;
		for (int i = 0; i < list.size(); i++) {
			exchange = false;
			for (int j = list.size() - 2; j >= i; j--) {
				if (( list.get(j + 1)).getUrlOrder().compareTo(
						(list.get(j)).getUrlOrder()) <= 0) {
					temp =  list.get(j + 1);
					list.set(j + 1, list.get(j));
					list.set(j, temp);
					exchange = true;
				}
			}
			if (!exchange)
				break;
		}
		return list;
	}*/
	// 天气接口
	/*public void weather() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8"); 
		JSONObject json =new JSONObject();
		JSONObject json2 = new JSONObject();
	
		PrintWriter out = null;
		String temp = "";
		String img2 = "";
		try {
			out = response.getWriter();
			URL url = new URL(
					"http://flash.weather.com.cn/wmaps/xml/shenzhen.xml");
			URLConnection uc = url.openConnection();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					uc.getInputStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			String datas = sb.toString();
			System.out.println(datas);
			// 创建一个新的字符串
			StringReader read = new StringReader(datas);
			// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
			InputSource source = new InputSource(read);
			// 创建一个新的SAXBuilder
			SAXBuilder sax = new SAXBuilder();
			Document doc = sax.build(source);
			Element root = doc.getRootElement();// 根节点
			List j = root.getChildren();
			Element et = null;
			for (int i = 0; i < j.size(); i++) {
				et = (Element) j.get(i);// 循环依次得到子元素
				temp = et.getAttributeValue("temNow") + "℃";
				img2 = et.getAttributeValue("state2"); // 图片序号
			}
			int i = Integer.parseInt(img2);
			String ss = WeatherUtil.getWeatherImg(i);// 图片
			img2 = IPUtil.getIp()+"/img/loginImg/weather/" + ss;// 图片地址
			// 得到日期
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_YEAR, 0);
			Date date = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
			String d = sdf.format(date);
			// 得到星期
			String week = getWeek(cal.get(Calendar.DAY_OF_WEEK));
			//返回
			json2.put("max", temp);
			json2.put("week", week);
			json2.put("date", d);
			json2.put("imgurl", img2);
			
			json.put("code", 1);
			json.put("weatherinfo", json2);
			out.print(json);
		} catch (Exception e) {
			json.put("code", -1);
			json.put("weatherinfo", json2);
			out.print(json);
			e.printStackTrace();
		}
	}*/
	
	/**
	 * 
	 * 摄像机url  app数据第三个请求  >Nf017
	 */
	
	public void cameraUrl(){
		org.json.JSONObject dataJson = DataUtil.dataParser(params);
		if(dataJson.has("areaid"))
			dataJson.getInt("areaid");
		
		HttpServletResponse response = ServletActionContext.getResponse();
		JSONObject j = DataUtil.sendJson();
		PrintWriter out= null;
		try {
			out= response.getWriter();
			JSONArray camera1 = new JSONArray();
			JSONArray camera2 = new JSONArray();
			org.json.JSONObject jo1 =new org.json.JSONObject();
			jo1.put("name", "市场部区域");
			jo1.put("key", "SN");
			jo1.put("value", "SCW-510-18FE34379E17");
			camera1.put(jo1);
			org.json.JSONObject jo2 =new org.json.JSONObject();
			jo2.put("name", "人事部区域");
			jo2.put("key", "SN");
			jo2.put("value", "SCW-510-18FE3437A449");
			camera1.put(jo2);
			org.json.JSONObject jo3 =new org.json.JSONObject();
			jo3.put("name", "研发部区域");
			jo3.put("key", "SN");
			jo3.put("value", "SCW-510-18FE3437A42A");
			camera1.put(jo3);
			org.json.JSONObject jo4 =new org.json.JSONObject();
			jo4.put("name", "开发部区域");
			jo4.put("key", "SN");
			jo4.put("value", "SCW-510-18FE3437A363");
			camera1.put(jo4);
			org.json.JSONObject jo5=new org.json.JSONObject();
			jo5.put("name", "财务部区域");
			jo5.put("key", "SN");
			jo5.put("value", "SCW-510-18FE34B9A205");
			camera1.put(jo5);
			org.json.JSONObject jo6 =new org.json.JSONObject();
			jo6.put("name", "公关部区域");
			jo6.put("key", "SN");
			jo6.put("value", "SCW-510-18FE3437A393");
			camera1.put(jo6);
			
			
			org.json.JSONObject jo7 =new org.json.JSONObject();
			jo7.put("name", "营业部区域");
			jo7.put("url", "http://hls.tongguantech.com/hlspage/shenda/mbvideo2.html");
			camera2.put(jo7);
			org.json.JSONObject jo8 =new org.json.JSONObject();
			jo8.put("name", "码头区域");
			jo8.put("url", "http://hls.tongguantech.com/hlspage/shenda/mbvideo3.html");
			camera2.put(jo8);
			org.json.JSONObject jo9 =new org.json.JSONObject();
			jo9.put("name", "养殖区域");
			jo9.put("url", "http://hls.tongguantech.com/hlspage/shenda/mbvideo4.html");
			camera2.put(jo9);
			org.json.JSONObject jo10 =new org.json.JSONObject();
			jo10.put("name", "展览区域");
			jo10.put("url", "http://hls.tongguantech.com/hlspage/shenda/mbvideo4.html");
			camera2.put(jo10);
			
			j.put("code", 1);
			j.put("comm", camera1.toString());
			j.put("indoor", camera2.toString());
		} catch (Exception e) {
			j.put("code", -1);
			out.print(j);
			e.printStackTrace();
		}
		out.print(j);
	}
	
	public void faultUrl(){
		
	}
public void linkUrl(){
	
	
}
	/*// 星期
	private String getWeek(int iw) {
		String weekStr = "";
		switch (iw) {
		case 1:
			weekStr = "星期天";
			break;
		case 2:
			weekStr = "星期一";
			break;
		case 3:
			weekStr = "星期二";
			break;
		case 4:
			weekStr = "星期三";
			break;
		case 5:
			weekStr = "星期四";
			break;
		case 6:
			weekStr = "星期五";
			break;
		case 7:
			weekStr = "星期六";
			break;
		default:
			break;
		}
		return weekStr;
	}*/
	
	
	
	
	public void setUrlService(UrlService urlService) {
		this.urlService = urlService;
	}
	public void setAreaid(int areaid) {
		this.areaid = areaid;
	}

	public void setParams(String params){
		this.params = params;
	}

	public UrlService getUrlService(){
		return urlService;
	}

	public void setVer(String ver) {
		this.ver = ver;
	}


	public void setVertype(int vertype) {
		this.vertype = vertype;
	}

	public void setVersionService(VersionService versionService) {
		this.versionService = versionService;
	}

	public void setAppType(int appType) {
		this.appType = appType;
	}
	
}
